package com.watson.spring.all.security.service.old.config;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.security.web.authentication.rememberme.JdbcTokenRepositoryImpl;
import org.springframework.security.web.authentication.rememberme.PersistentTokenRepository;

import javax.sql.DataSource;

/**
 * @author watson
 * @date 2021/5/27 15:14
 * @Description: 自定义实现类
 **/
@Configuration
public class SecurityConfigNew extends WebSecurityConfigurerAdapter {
    @Autowired
    private UserDetailsService userDetailsService;
    //注入数据源
    @Autowired
    private DataSource dataSource;

    @Bean//记住我功能
    public PersistentTokenRepository getPersistentTokenRepository() {
        JdbcTokenRepositoryImpl jdbcTokenRepository = new JdbcTokenRepositoryImpl();
        jdbcTokenRepository.setDataSource(dataSource);
        //是否需要创建表
//        jdbcTokenRepository.setCreateTableOnStartup(true);

        return jdbcTokenRepository;
    }


    @Override
    protected void configure(AuthenticationManagerBuilder auth) throws Exception {
        auth.userDetailsService(userDetailsService).passwordEncoder(password());
    }

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        //配置没有权限访问跳转自定义页面
        http.exceptionHandling().accessDeniedPage("/unauth.html");
        http.logout()
                .logoutUrl("/logout")
                .logoutSuccessUrl("/test/hello")
                .permitAll()
                //记住我功能，自动登陆
                .and().rememberMe()
                .tokenRepository(getPersistentTokenRepository())
                .tokenValiditySeconds(20) //过去时间，秒为单位
                .userDetailsService(userDetailsService);

        http
                .formLogin()//自定义编写的登陆页面
                .loginPage("/login.html") //登陆页面路径
                .loginProcessingUrl("/user/login")  //登陆访问路径
                .defaultSuccessUrl("/success.html").permitAll() //登陆成功之后，跳转路径
                .and().authorizeRequests()
                .antMatchers("/", "/test/hello", "/user/login")
                .permitAll() //设置哪些路径可以直接访问不需要认证
//                .antMatchers("/test/index").hasAuthority("admins")
//                .antMatchers("/test/index").hasAnyAuthority("admins,manager")
//                .antMatchers("/test/index").hasRole("admins,manager")
                .anyRequest().authenticated()
                .and().csrf().disable(); //关闭csrf 防护


    }

    @Bean
    PasswordEncoder password() {
        return new BCryptPasswordEncoder();
    }

}
